package com.cropdoctor.service.impl;

import com.cropdoctor.mapper.VideoSummaryMapper;
import com.cropdoctor.model.dto.VideoSummaryDto;
import com.cropdoctor.model.dto.VideoSummaryRequestDto;
import com.cropdoctor.model.po.VideoSummaryPo;
import com.cropdoctor.service.VideoSummaryService;
import com.cropdoctor.util.AiSummaryUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * 视频总结服务实现类
 * 
 * @author Augment Agent
 * @date 2024-12-26
 */
@Service
@Slf4j
public class VideoSummaryServiceImpl implements VideoSummaryService {
    
    @Autowired
    private VideoSummaryMapper videoSummaryMapper;
    
    @Autowired
    private AiSummaryUtil aiSummaryUtil;
    
    @Override
    public String generateSummary(VideoSummaryRequestDto request) {
        log.info("开始生成视频总结，标题: {}, 描述: {}", request.getTitle(), request.getDescription());
        
        try {
            String summary = aiSummaryUtil.generateVideoSummary(
                request.getTitle(),
                request.getDescription(),
                request.getMt(),
                request.getSt(),
                request.getVideoUrl()
            );
            
            log.info("视频总结生成成功，长度: {}", summary.length());
            return summary;
            
        } catch (Exception e) {
            log.error("生成视频总结失败", e);
            throw new RuntimeException("生成视频总结失败: " + e.getMessage());
        }
    }
    
    @Override
    public VideoSummaryDto saveSummary(Long courseId, String summary, String provider) {
        log.info("保存视频总结，课程ID: {}, 提供商: {}", courseId, provider);
        
        try {
            VideoSummaryPo po = new VideoSummaryPo();
            po.setCourseId(courseId);
            po.setSummary(summary);
            po.setProvider(provider);
            po.setGenerateTime(LocalDateTime.now());
            po.setCreateTime(LocalDateTime.now());
            po.setUpdateTime(LocalDateTime.now());
            
            videoSummaryMapper.insert(po);
            
            VideoSummaryDto dto = new VideoSummaryDto();
            BeanUtils.copyProperties(po, dto);
            
            log.info("视频总结保存成功，ID: {}", po.getId());
            return dto;
            
        } catch (Exception e) {
            log.error("保存视频总结失败", e);
            throw new RuntimeException("保存视频总结失败: " + e.getMessage());
        }
    }
    
    @Override
    public VideoSummaryDto getLatestSummaryByCourseId(Long courseId) {
        log.info("查询课程最新总结，课程ID: {}", courseId);
        
        try {
            VideoSummaryPo po = videoSummaryMapper.selectLatestByCourseId(courseId);
            if (po == null) {
                log.info("未找到课程总结，课程ID: {}", courseId);
                return null;
            }
            
            VideoSummaryDto dto = new VideoSummaryDto();
            BeanUtils.copyProperties(po, dto);
            
            log.info("查询到课程总结，ID: {}", po.getId());
            return dto;
            
        } catch (Exception e) {
            log.error("查询课程总结失败", e);
            throw new RuntimeException("查询课程总结失败: " + e.getMessage());
        }
    }
}
